/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.List;
import java.util.Set;
import models.Answer;
import models.Course;
import models.Test;
import models.Question;
import models.TestResult;
import models.User;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Aster
 */
public class ProgressTestDao {

    private final Session session;

    public ProgressTestDao(Session session) {
        this.session = session;
    }

    public List<Test> findAllTests() {
        Transaction tx = session.beginTransaction();
        List<Test> list = session.createQuery("from Test").list();
        tx.commit();
        return list;
    }

    public List<Test> findAllTests(Long courseID) {
        Transaction tx = session.beginTransaction();
        List<Test> list = session.createQuery("from Test where course=" + courseID).list();
        tx.commit();
        return list;
    }

    public void saveTest(Test test) {
        Transaction tx = session.beginTransaction();
        session.save(test);
        tx.commit();
    }

    public void updateTest(Test test) {
        Transaction tx = session.beginTransaction();
        session.update(test);
        tx.commit();
    }

    public Test getTest(long testID) {
        return (Test) session.get(Test.class, testID);
    }

    public void deleteTest(Test test) {
        Transaction tx = session.beginTransaction();
        try {
            Set<Question> questions = test.getQuestions();
            for (Question q : questions) {
                Query query = session.createQuery("delete from Answer where parentquestion_questionid = " + q.getQuestionID());
                query.executeUpdate();
            }
        } catch (Exception e) {
            System.out.println("no questions to delete" + e);
        }
        
        Query query = session.createQuery("delete from TestResult where testID = " + test.getTestID());
                query.executeUpdate();

        session.delete(test);
        tx.commit();
    }

    public Question getQuestion(long questionID) {
        return (Question) session.get(Question.class, questionID);
    }

    public void saveQuestion(Question question) {
        Transaction tx = session.beginTransaction();
        session.save(question);
        tx.commit();
    }

    public void updateQuestion(Question question) {
        Transaction tx = session.beginTransaction();
        session.update(question);
        tx.commit();
    }

    public void deleteQuestion(Question question, String id) {
        Test t = getTest(Long.parseLong(id));
        t.deleteAQuestion(question);
        updateTest(t);

        Transaction tx = session.beginTransaction();
        Query q = session.createQuery("delete from Answer where parentquestion_questionid = " + question.getQuestionID());
        q.executeUpdate();
        session.delete(question);
        tx.commit();
    }

    public void saveAnswer(Answer answer) {
        Transaction tx = session.beginTransaction();
        session.save(answer);
        tx.commit();
    }

    public void updateAnswer(Answer answer) {
        Transaction tx = session.beginTransaction();
        session.update(answer);
        tx.commit();
    }

    public List<Answer> getAnswers(Long questionID) {
        Transaction tx = session.beginTransaction();
        List<Answer> list = session.createQuery("from Answer where parentquestion_questionid = " + questionID).list();
        tx.commit();
        return list;
    }

    public Answer getAnswer(Long answerID) {
        return (Answer) session.get(Answer.class, answerID);
    }

    public void saveTestResult(TestResult TestResult) {
        Transaction tx = session.beginTransaction();
        session.save(TestResult);
        tx.commit();
    }

    public List<TestResult> getTestResults(Test test, User user) {
        Transaction tx = session.beginTransaction();
        List<TestResult> list = session.createQuery("from TestResult where userID=" + user.getUserID() + "and testID=" + test.getTestID()).list();
        tx.commit();
        return list;
    }
}
